
# class BaseOrderManager(models.Manager):
#     def insert_order(self, single, create_time, cash=None):
#         """
#         插入数据
#         :param create_time: 成交时间
#         :param single: 标识
#         :param cash: 金额  如果是贷款类（保险类）的产品，就需要知道贷了多少钱，而且类型是百分比
#         :return:
#         """
#         price = PriceUpdate.objects.filter(product=single.product).first()
#         if cash and price.price_type == 2:  # 贷款类产品
#             income = cash * float(price.price)
#         elif not cash and price.price_type == 1:  # 信用卡类产品，手填固定值
#             income = price.price
#         else:
#             raise Exception("请检查产品金额类型")
#         if single.applicant.tel == single.b_user.tel:
#             # 如果申请人手机号和被推荐人手机号一致 说明是上级推荐
#             salesman = single.t_user
#         else:
#             # 代替申请人办卡，那么佣金应该给代办业务员
#             salesman = single.b_user
#         order, created = self.get_or_create(
#             product=single.product,
#             salesman=salesman,
#             single=single,
#             price=price,
#             income=income,
#             create_time=create_time,
#         )
#         order.is_close = 2
#         order.save()
#         if created:
#             TeamInCome.objects.insert_order(order, cash=cash)
#
#     def sum_income(self, salesman, p_id=None):
#         """
#         已提现，可提现，待提现 数量
#         :return:
#         """
#         info = self.filter(salesman=salesman)
#         if p_id:
#             info = info.filter(product_id=p_id)
#         sum_is_close_1 = info.filter(is_close=1).aggregate(sum_is_close_1=Sum("income")).get("sum_is_close_1")
#         sum_is_close_2 = info.filter(is_close=2).aggregate(sum_is_close_2=Sum("income")).get("sum_is_close_2")
#         sum_is_close_3 = info.filter(is_close=3).aggregate(sum_is_close_3=Sum("income")).get("sum_is_close_3")
#         sum_is_close_1 = sum_is_close_1 if sum_is_close_1 else 0
#         sum_is_close_2 = sum_is_close_2 if sum_is_close_2 else 0
#         sum_is_close_3 = sum_is_close_3 if sum_is_close_3 else 0
#         return {
#             "sum_is_close_1": sum_is_close_1,
#             "sum_is_close_2": sum_is_close_2,
#             "sum_is_close_3": sum_is_close_3
#         }
#
#     def __order_close_type(self, salesman, is_close=None):
#         info = self.filter(salesman=salesman)
#         if is_close:
#             info = info.filter(is_close=is_close)
#         print(info.query)
#         return info
#
#     def id_list_and_close_sum(self, salesman, is_close=2):
#         info = self.__order_close_type(salesman, is_close=is_close)
#         amount = info.aggregate(is_close=Sum("income")).get("is_close")
#         amount = amount if amount else 0
#         amount = float(amount) * 100
#         amount = ceil(amount)  # 分
#         id_list = info.values_list('id', flat=True)
#         return id_list, amount
#
#     def update_close(self, id_list):
#         if id_list:
#             self.filter(id__in=id_list).update(is_close=1)
#
#
# class BaseOrder(models.Model):
#     """
#     个人订单（因为有提现的状态，所以只能是一个用户产生了订单，分三个订单即自己，上级，上上级）
#     """
#     CLOSE_TYPE = ((1, "已提现"), (2, "可提现"), (3, "待提现"))
#     single = models.ForeignKey("sales_man.Single", verbose_name="申请标识", db_index=True)
#     salesman = models.ForeignKey('sales_man.SalesMan', verbose_name="直推业务员", help_text="有可能是自己，也有可能是上级")
#     product = models.ForeignKey('product.Product', verbose_name="产品")
#     price = models.ForeignKey("PriceUpdate", verbose_name="价格")
#     create_time = models.DateTimeField(verbose_name="创建时间(银行返回的时间)")
#     income = models.DecimalField(verbose_name="收益", max_digits=7, decimal_places=2, default=0)
#     is_close = models.IntegerField(verbose_name="结算状态", default=3, choices=CLOSE_TYPE, db_index=True)
#     close_time = models.DateTimeField(verbose_name="结算日期", blank=True, null=True)
#     objects = BaseOrderManager()
#
#     class Meta:
#         verbose_name = "订单表"
#         verbose_name_plural = verbose_name
#
#     def __str__(self):
#         return "%s:%s:%s" % (self.salesman.name, self.product.name, self.income)
#
#
# class TeamInComeManager(models.Manager):
#     def update_close(self, id_list):
#         if id_list:
#             self.filter(id__in=id_list).update(is_close=1)
#
#     def __insert_order(self, income, order, leader, level):
#         """
#         插入数据
#         :param income:
#         :param order:
#         :param leader:
#         :param level: 增加一个标记，是由自己下面几级带来的效益
#         :return:
#         """
#         return self.create(
#             income=income,
#             salesman=leader,
#             order=order,
#             level=level,
#             create_time=order.create_time,
#             is_close=2
#         )
#
#     def insert_order(self, self_order, cash=None):
#         """
#         个人订单
#         分别判断是否有上级和上上级，如果有加入收益当中
#         :param self_order: 自己的订单
#         :param cash: 自己的订单
#         :return:
#         """
#         # self_order = BaseOrder
#         _leader = self_order.salesman.leader
#         price_type = self_order.price.price_type
#         if _leader:
#             if price_type == 1:
#                 income = self_order.price.leader_price
#             else:
#                 income = float(cash) * float(self_order.price.leader_price)
#             self.__insert_order(income, self_order, _leader, 1)
#
#             _leader_leader = _leader.leader
#             if _leader_leader:
#                 if price_type == 1:
#                     income = self_order.price.leader_leader_price
#                 else:
#                     income = float(cash) * float(self_order.price.leader_leader_price)
#                 self.__insert_order(income, self_order, _leader_leader, 2)
#
#     def all_order(self, salesman):
#         """
#         所有订单
#         :param salesman:
#         :return:
#         """
#         return self.filter(salesman=salesman)
#
#     def sum_income(self, salesman, p_id=None):
#         """
#         已提现，可提现，待提现 数量
#         :return:
#         """
#         info = self.filter(salesman=salesman)
#         if p_id:
#             info = info.filter(product_id=p_id)
#         sum_is_close_1 = info.filter(is_close=1).aggregate(sum_is_close_1=Sum("income")).get("sum_is_close_1")
#         sum_is_close_2 = info.filter(is_close=2).aggregate(sum_is_close_2=Sum("income")).get("sum_is_close_2")
#         sum_is_close_3 = info.filter(is_close=3).aggregate(sum_is_close_3=Sum("income")).get("sum_is_close_3")
#         sum_is_close_1 = sum_is_close_1 if sum_is_close_1 else 0
#         sum_is_close_2 = sum_is_close_2 if sum_is_close_2 else 0
#         sum_is_close_3 = sum_is_close_3 if sum_is_close_3 else 0
#         return {
#             "sum_is_close_1": sum_is_close_1,
#             "sum_is_close_2": sum_is_close_2,
#             "sum_is_close_3": sum_is_close_3
#         }
#
#     def __order_close_type(self, salesman, is_close=None):
#         info = self.filter(salesman=salesman)
#         if is_close:
#             info = info.filter(is_close=is_close)
#         return info
#
#     def id_list_and_close_sum(self, salesman, is_close=2):
#         info = self.__order_close_type(salesman, is_close=is_close)
#         amount = info.aggregate(is_close=Sum("income")).get("is_close")
#         amount = amount if amount else 0
#         amount = float(amount) * 100
#         amount = ceil(amount)  # 分
#         id_list = info.values_list('id', flat=True)
#         return id_list, amount
#
#
# class TeamInCome(models.Model):
#     """
#     团队收入, 团队贡献
#     用户 A给上级B带来的收益记录一条， 用户A给上上级C带来的收益记录一条
#         salesman 指的是B 和 C
#     """
#     CLOSE_TYPE = ((1, "已提现"), (2, "可提现"), (3, "待提现"))
#     LEVEL = ((1, "下级"), (2, "下下级"))
#     salesman = models.ForeignKey('sales_man.SalesMan', verbose_name="自己")
#     order = models.ForeignKey('BaseOrder', verbose_name="对应的订单")
#     level = models.IntegerField(default=2, verbose_name="给我带来收益的级别", choices=LEVEL)
#     income = models.DecimalField(verbose_name="收益", max_digits=7, decimal_places=2, default=0)
#     is_close = models.IntegerField(verbose_name="结算状态", choices=CLOSE_TYPE, default=3)
#     create_time = models.DateTimeField(verbose_name="创建时间(银行返回的时间)")
#     objects = TeamInComeManager()
#
#     class Meta:
#         verbose_name = "团队收入"
#         verbose_name_plural = verbose_name
#         ordering = ["-create_time"]
#
